package sprint1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.Math;
import java.util.Scanner;

/**
 * 
 * @author Michael de Kaste
 *
 */
public class BattleSystem {
	private String hpBalkmax = "||||||||||||||||||||||||||||||||||||||||||||||||||";
	private String hpBalkmin = "                                                  ";

	private double hpMaxME;
	private double hpNowME;
	private double armourME;
	private double powerME;
	private int dicesizeME;
	private int speedME;
	private String hpStringME;
	private double critChanceME;
	private double critDamageME;

	private double hpMaxHE;
	private double hpNowHE;
	private double armourHE;
	private double powerHE;
	private int dicesizeHE;
	private int speedHE;
	private String hpStringHE;
	private double critChanceHE;
	private double critDamageHE;

	Scanner scanner = new Scanner(System.in);

	public BattleSystem(Alien alien, Character character) {
		setHpMaxHE(alien.getHpMax());
		setHpNowHE(hpMaxHE);
		setArmourHE(alien.getArmour());
		setPowerHE(alien.getPower());
		setDicesizeHE(alien.getDicesize());
		setSpeedHE(alien.getSpeed());
		setCritChanceHE(alien.getCritChance());
		setCritDamageHE(alien.getCritDamage());
		setHpMaxME(character.getHpMax());
		setHpNowME(character.getHpNow());
		setArmourME(character.getArmour());
		setPowerME(character.getPower());
		setDicesizeME(character.getDicesize());
		setSpeedME(character.getSpeed());
		setCritDamageME(character.getCritDamage());
		setCritChanceME(character.getCritChance());
		hpStringHE = hpBalkmax.substring(0,
				(int) (50 * hpNowHE / hpMaxHE))
				+ hpBalkmin.substring(0, (50 - (int) (50 * hpNowHE / hpMaxHE)));
		hpStringME = hpBalkmax.substring(0,
				(int) (50 * hpNowME / hpMaxME))
				+ hpBalkmin.substring(0, (50 - (int) (50 * hpNowME / hpMaxME)));

	}

	static void printOut(String filename) {
		BufferedReader in = null;
		try {
			File file = new File(filename);
			in = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			System.out.println("The file doesn't exist.");
			System.exit(0);
		} catch (IOException e) {
			System.out.println("I/O Error");
			System.exit(0);
		}
		try {
			while (in.ready()) {
				System.out.println(in.readLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void fightME() {
		printOut("alien.txt");
		System.out.println("Je probeert te aanvallen..."); // begin output prompt
		int AttackRolME = (int) (1 + (dicesizeME * Math.random())); // Jij gooit je dobbelsteen.
		int AttackRolHE = (int) (1 + (dicesizeHE * Math.random())); // Hij gooit zijn dobbelsteen.
		System.out.println((AttackRolME >= AttackRolHE) ? "Raak!" : "Mis!"); // output "Raak!" bij true, "Mis!" bij
																				// false
		if (AttackRolME >= AttackRolHE) { // Als je raak gooit...
			hpNowHE -= (powerME
					* (((100 * Math.random() + 1) <= critChanceME) ? critDamageME
							: 1) * (1 - (armourHE / 100))); // de kracht van je aanval wordt eerst tegengehouden door
															// wat armour en vervolgens gaat dat van zijn hp af.
			hpNowHE = ((hpNowHE > 0) ? hpNowHE : 0.0); // zijn hp is een double bij groter dan 0, anders wordt de waarde
														// 0.
			hpStringHE = hpBalkmax.substring(0, (int) (50 * hpNowHE / hpMaxHE))
					+ hpBalkmin.substring(0,
							(50 - (int) (50 * hpNowHE / hpMaxHE)));
			System.out
					.println("Je aanval deed "
							+ (powerME
									* (((100 * Math.random() + 1) <= critChanceME) ? critDamageME
											: 1) * (1 - (armourHE / 100)))
							+ " schade!"); // output de schade
		}
		System.out.println("1: ga verder!");
		scanner.next();
	}

	public void fightHE() {
		printOut("alien.txt");
		System.out.println("Hij probeert te aanvallen...");
		int AttackRolME = (int) (1 + (dicesizeHE * Math.random()));
		int AttackRolHE = (int) (1 + (dicesizeME * Math.random()));
		System.out.println((AttackRolHE >= AttackRolME) ? "Raak!" : "Mis!");
		if (AttackRolHE >= AttackRolME) {
			hpNowME -= (powerHE
					* (((100 * Math.random() + 1) <= critChanceHE) ? critDamageHE
							: 1) * (1 - (armourME / 100)));
			hpNowME = ((hpNowME > 0) ? hpNowME : 0.0);
			hpStringME = hpBalkmax.substring(0, (int) (50 * hpNowME / hpMaxME))
					+ hpBalkmin.substring(0,
							(50 - (int) (50 * hpNowME / hpMaxME)));
			System.out
					.println("zijn aanval deed "
							+ (powerHE
									* (((100 * Math.random() + 1) <= critChanceHE) ? critDamageHE
											: 1) * (1 - (armourME / 100)))
							+ " schade!");
		}
		System.out.println("1: ga verder!");
		scanner.next();
	}

	public void simuleer(Character character) {
		while ((getHpME() > 0) && (getHpHE() > 0)) {
			printOut("alien.txt");

			int lenME = 3 - (int) Math.log10(hpNowME / hpMaxME * 100);
			System.out.println("Mijn hp \t["
					+ hpStringME.substring(0, 22 + lenME)
					+ Math.round(hpNowME / hpMaxME * 100) + "%"
					+ hpStringME.substring(27, 50) + "]");

			int lenHE = 3 - (int) Math.log10(hpNowHE / hpMaxHE * 100);
			System.out.println("Zijn hp \t["
					+ hpStringHE.substring(0, 22 + lenHE)
					+ Math.round(hpNowHE / hpMaxHE * 100) + "%"
					+ hpStringHE.substring(27, 50) + "]");

			System.out.println("1: Aanvallen!");
			System.out.println("2: Gebruik een Item!");
			int i = scanner.nextInt();
			switch (i) {
			case 1:
				if (getSpeedME() >= getSpeedHE()) {
					fightME();
					if (getHpHE() <= 0)
						break;
					fightHE();
				} else {
					fightHE();
					if (getHpME() <= 0)
						break;
					fightME();
				}
			}
		}
		//
		System.out.println((getHpHE() == 0) ? "Yeeeeeeeeeuh! je hebt gewonnen!"
				: "Ah bagger, je verloor!");

		character.setHpNow(hpNowME);
	}

	public void setHpBalkmax(String hpBalkmax) {
		this.hpBalkmax = hpBalkmax;
	}

	public void setHpBalkmin(String hpBalkmin) {
		this.hpBalkmin = hpBalkmin;
	}

	public void setHpMaxME(double hpMaxME) {
		this.hpMaxME = hpMaxME;
	}

	public void setHpNowME(double hpNowME) {
		this.hpNowME = hpNowME;
	}

	public void setArmourME(double armourME) {
		this.armourME = armourME;
	}

	public void setPowerME(double powerME) {
		this.powerME = powerME;
	}

	public void setDicesizeME(int dicesizeME) {
		this.dicesizeME = dicesizeME;
	}

	public void setSpeedME(int speedME) {
		this.speedME = speedME;
	}

	public void setHpStringME(String hpStringME) {
		this.hpStringME = hpStringME;
	}

	public void setCritChanceME(double critChanceME) {
		this.critChanceME = critChanceME;
	}

	public void setCritDamageME(double critDamageME) {
		this.critDamageME = critDamageME;
	}

	public void setHpMaxHE(double hpMaxHE) {
		this.hpMaxHE = hpMaxHE;
	}

	public void setHpNowHE(double hpNowHE) {
		this.hpNowHE = hpNowHE;
	}

	public void setArmourHE(double armourHE) {
		this.armourHE = armourHE;
	}

	public void setPowerHE(double powerHE) {
		this.powerHE = powerHE;
	}

	public void setDicesizeHE(int dicesizeHE) {
		this.dicesizeHE = dicesizeHE;
	}

	public void setSpeedHE(int speedHE) {
		this.speedHE = speedHE;
	}

	public void setHpStringHE(String hpStringHE) {
		this.hpStringHE = hpStringHE;
	}

	public void setCritChanceHE(double critChanceHE) {
		this.critChanceHE = critChanceHE;
	}

	public void setCritDamageHE(double critDamageHE) {
		this.critDamageHE = critDamageHE;
	}

	private int getSpeedHE() {
		return speedHE;
	}

	private int getSpeedME() {
		return speedME;
	}

	private double getHpHE() {
		return hpNowHE;
	}

	private double getHpME() {
		return hpNowME;
	}
}
